﻿/*
  Please keep these information:
  From: http://wpfhelpertools.codeplex.com/ 
  Author: http://www.cnblogs.com/xiaokang088 
  Email: xiaokang060@hotmail.com
*/
using System;
using System.IO;
using System.Text;
using System.Windows;

namespace CodeGenerate
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        #region memeber variable
        /// <summary>
        /// read template path
        /// </summary>
        readonly string path = AppDomain.CurrentDomain.BaseDirectory + "PropertyTemplate.txt";
        /// <summary>
        /// template content
        /// </summary>
        readonly string template = string.Empty;
        /// <summary>
        /// replace content
        /// </summary>
        string content = string.Empty;
        #endregion

        #region constructor
        public MainWindow()
        {
            InitializeComponent();

            if (!File.Exists(path))
            {
                MessageBox.Show("Template file is missing");
                this.Close();
                return;
            }

            try
            {
                template = File.ReadAllText(path);
            }
            catch (Exception ex)
            {
                MessageBox.Show("can't read template file" + ex.Message);
                this.Close();
            }
        }
        #endregion

        #region event handle
        private void btnWrite_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (string.IsNullOrEmpty(tbRequest.Text))
                {
                    string propertyName = tbPropertyName.Text.Trim();
                    string ownerName = tbOwnerName.Text.Trim();
                    string typeName = cbType.Text;
                    if (string.IsNullOrEmpty(propertyName) || string.IsNullOrEmpty(ownerName) || string.IsNullOrEmpty(typeName))
                    {
                        MessageBox.Show("Information is not incomplete");
                        return;
                    }

                    content = template.Replace("{PropertyName}", propertyName);
                    content = content.Replace("{TypeName}", typeName);
                    content = content.Replace("{OwnerName}", ownerName);

                    tbResult.Text = content;
                    Clipboard.SetText(content);
                }
                else
                {
                    StringBuilder builder = new StringBuilder();
                    string request = tbRequest.Text;
                    if (string.IsNullOrEmpty(request)) return;
                    var requests = request.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);

                    foreach (string str in requests)
                    {
                        var parms = str.Split('|');
                        string propertyName = parms[0];
                        string ownerName = parms[2];
                        string typeName = parms[1];

                        content = template.Replace("{PropertyName}", propertyName);
                        content = content.Replace("{TypeName}", typeName);
                        content = content.Replace("{OwnerName}", ownerName);
                        builder.Append(content);
                        builder.AppendLine();
                    }

                    tbResult.Text = builder.ToString();
                    Clipboard.SetText(builder.ToString());
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnAdd_Click(object sender, RoutedEventArgs e)
        {
            string propertyName = tbPropertyName.Text.Trim();
            string ownerName = tbOwnerName.Text.Trim();
            string typeName = cbType.Text;
            if (string.IsNullOrEmpty(propertyName) || string.IsNullOrEmpty(ownerName) || string.IsNullOrEmpty(typeName))
            {
                MessageBox.Show("Information is not incomplete");
                return;
            }
            string requireFormat = "{0}|{1}|{2}";
            string require = string.Format(requireFormat, propertyName, typeName, ownerName);
            if (tbRequest.Text.Contains(require)) return;

            if (string.IsNullOrEmpty(tbRequest.Text))
                tbRequest.Text = require;
            else
                tbRequest.Text = tbRequest.Text + "\r\n" + require;
        }
        #endregion
    }
}
